package solution._0070._0074;

/**
 * Created by chen_snow
 * Date: 2021/6/20 22:16
 * Description: 搜索二维矩阵
 */
public class Solution {

    public static void main(String[] args) {
        searchMatrix(new int[][]{{1,3,5,7}, {10,11,16,20},{23,30,34,60}},3);
    }

    public static boolean searchMatrix(int[][] matrix, int target) {
        int row = matrix.length;
        int column = matrix[0].length;

        int start = 0;
        int end = row * column - 1;

        while (start <= end) {
            int middle = (end - start) / 2 + start;
            int num = matrix[middle / column][middle % column];

            if (num == target) {
                return true;
            } else if (num < target) {
                start = middle + 1;
            } else {
                end = middle - 1;
            }
        }

        return false;
    }

}
